package main

import (
	"encoding/json"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
)

func appicon() {
	//生成相应的文件
	imgSet := appIconSet()
	dirName := "AppIcon.appiconset"
	os.Mkdir(dirName, 0755)
	contentPath := dirName + "/Contents.json"
	buff, err := json.Marshal(imgSet)
	if err == nil {
		ioutil.WriteFile(contentPath, buff, 0755)
	}
	filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
		if info.IsDir() {
			return nil
		}
		if !strings.HasSuffix(path, ".png") {
			return nil
		}
		start := strings.LastIndex(path, "x")
		end := strings.LastIndex(path, ".")
		size := path[start+1 : end]
		fileName := dirName + "/AppIcon_" + size + "x" + size + ".png"
		os.Rename(path, fileName)
		return nil
	})
}

func appIconSet() *ImageSet {
	imgSet := NewImageSet()

	//20x20的几个图片
	imgItem := NewImageItem()
	imgItem.Size = "20x20"
	imgItem.Idiom = "iphone"
	imgItem.Filename = "AppIcon_40x40.png"
	imgItem.Scale = "2x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "20x20"
	imgItem.Idiom = "iphone"
	imgItem.Filename = "AppIcon_60x60.png"
	imgItem.Scale = "3x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "29x29"
	imgItem.Idiom = "iphone"
	imgItem.Filename = "AppIcon_58x58.png"
	imgItem.Scale = "2x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "29x29"
	imgItem.Idiom = "iphone"
	imgItem.Filename = "AppIcon_87x87.png"
	imgItem.Scale = "3x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "40x40"
	imgItem.Idiom = "iphone"
	imgItem.Filename = "AppIcon_80x80.png"
	imgItem.Scale = "2x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "40x40"
	imgItem.Idiom = "iphone"
	imgItem.Filename = "AppIcon_120x120.png"
	imgItem.Scale = "3x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "60x60"
	imgItem.Idiom = "iphone"
	imgItem.Filename = "AppIcon_120x120.png"
	imgItem.Scale = "2x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "60x60"
	imgItem.Idiom = "iphone"
	imgItem.Filename = "AppIcon_180x180.png"
	imgItem.Scale = "3x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "20x20"
	imgItem.Idiom = "ipad"
	imgItem.Filename = "AppIcon_20x20.png"
	imgItem.Scale = "1x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "20x20"
	imgItem.Idiom = "ipad"
	imgItem.Filename = "AppIcon_40x40.png"
	imgItem.Scale = "2x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "29x29"
	imgItem.Idiom = "ipad"
	imgItem.Filename = "AppIcon_29x29.png"
	imgItem.Scale = "1x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "29x29"
	imgItem.Idiom = "ipad"
	imgItem.Filename = "AppIcon_58x58.png"
	imgItem.Scale = "2x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "40x40"
	imgItem.Idiom = "ipad"
	imgItem.Filename = "AppIcon_40x40.png"
	imgItem.Scale = "1x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "40x40"
	imgItem.Idiom = "ipad"
	imgItem.Filename = "AppIcon_80x80.png"
	imgItem.Scale = "2x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "76x76"
	imgItem.Idiom = "ipad"
	imgItem.Filename = "AppIcon_76x76.png"
	imgItem.Scale = "1x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "76x76"
	imgItem.Idiom = "ipad"
	imgItem.Filename = "AppIcon_152x152.png"
	imgItem.Scale = "2x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "83.5x83.5"
	imgItem.Idiom = "ipad"
	imgItem.Filename = "AppIcon_167x167.png"
	imgItem.Scale = "2x"
	imgSet.AddImageItem(imgItem)

	imgItem = NewImageItem()
	imgItem.Size = "1024x1024"
	imgItem.Idiom = "ios-marketing"
	imgItem.Filename = "AppIcon_1024x1024.png"
	imgItem.Scale = "1x"
	imgSet.AddImageItem(imgItem)

	return imgSet
}
